package libro.cap04.contratos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

public class EmpDAOImpl {

	public Collection<EmpDTO> buscarUnDept(int deptno) {

		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = UConnection.getConnection();
			String sql = "SELECT empno, ename, hiredate, deptno FROM empleados WHERE deptno = ?";
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, deptno);
			rs = pstm.executeQuery();

			ArrayList<EmpDTO> ret = new ArrayList<EmpDTO>();
			EmpDTO dto = new EmpDTO();

			while (rs.next()) {

				dto = new EmpDTO();
				dto.setEmpno(rs.getInt("empno"));
				dto.setEname(rs.getString("ename"));
				dto.setHiredate(rs.getDate("hiredate"));
				dto.setDeptno(rs.getInt("deptno"));
				ret.add(dto);
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		finally {
			try {
				if (rs != null)
					rs.close();
				if (pstm != null)
					pstm.close();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);

			}
		}

	}
}
